package com.lei.demo.activiti.spring.service.task;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Assert;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AutomaticTaskTest {
	
	private static ApplicationContext context;
	
	private static ProcessEngine processEngine;
	private static RepositoryService repositoryService;
	private static RuntimeService runtimeService;
	private static String deploymentId;
	
	public static void main(String[] args) throws Exception {
		
		System.out.println( "start" );
		
		AutomaticTaskTest.initialize();
		
		AutomaticTaskTest.test();
		
		System.out.println( "end" );
	}
	
	 public static void test() {
		 	
	        ProcessInstance pi = runtimeService.startProcessInstanceByKey("serviceTaskProcess");
	        System.out.println("ProcessInstance Id："+pi.getId());
	        System.out.println("ProcessInstance ActivitiId："+pi.getActivityId());
	        
	        Assert.assertEquals(true, pi.isEnded());
	 }
	

	protected static void initialize() throws Exception {
		
		context = new ClassPathXmlApplicationContext("spring-activiti.xml");
		
		System.out.println("创建Activiti流程引擎，Activiti主接口");
		processEngine = (ProcessEngine) context.getBean("processEngine");
		System.out.println("Activiti主接口创建完毕，processEngine Name:"+processEngine.getName());
		
		System.out.println("获取Activiti的资源操作接口--开始");
        repositoryService = processEngine.getRepositoryService();
        System.out.println("获取Activiti的资源操作接口--完毕");
        
        System.out.println("获得运行时操作接口--开始");
        runtimeService = processEngine.getRuntimeService();
        System.out.println("获得运行时操作接口--完毕");
		
        System.out.println("部署流程，创建数据库表--开始");
		deploymentId = repositoryService.createDeployment()  
	            .addClasspathResource("diagrams/ServiceTaskProcess.bpmn")  
	            .deploy().getId();
		System.out.println("部署流程，创建数据库表--完毕");
	}
    
	protected static void destroy() throws Exception {  
        repositoryService.deleteDeployment(deploymentId, true);  
    }
	
	

}
